package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Validated
@Slf4j
@Api(tags = "相册的控制器")
@RequestMapping("/albums")
@RestController
public class AlbumController {
    @Autowired
    private IAlbumService service;

    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    public JsonResult<Void> add(@Valid AlbumAddNewDTO add) {
        log.debug("开始处理添加相册，数据是：{}",add);
        service.addNew(add);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除相册")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("需要删除的id是：{}",id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新相册")
    public JsonResult<Void> updateInfoById(@Range(min = 1,max = 10000,message = "删除相册失败，非法相册ID")
            @PathVariable Long id,AlbumUpdateDTO updateDTO){
        log.debug("需要更新的id是：{},更新参数是：{}",id,updateDTO);
        service.updateInfoById(id,updateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("根据id查询详情")
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable Long id){
        log.debug("需要查询详情的id是：{}",id);
        AlbumStandardVO standardVO = service.getStandardById(id);
        return JsonResult.ok(standardVO);
    }

    @GetMapping("/list")
    @ApiOperation("查询列表")
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理查询相册列表，无参数");
        List<AlbumListItemVO> list = service.getListItem();
        return JsonResult.ok(list);
    }

    // http://localhost:8080/album/name
    @GetMapping("/{name:[a-z]+}")
    public String getStandardByName(@PathVariable String name){
        return "根据测试name【"+name+"】查询删除详细";
    }

}
